﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Collider))]
public class PropTakeDamage : MonoBehaviour {

    public int m_Health = 100;
    public GameObject m_DamagedState;
    public Transform m_DamagedStateTransform;
    public bool canDamageByRush;

	void Start () {
		
	}
	
	void Update () {
	}

    private void OnCollisionEnter(Collision collision)
    {
        if (!canDamageByRush) return;

        OnTakeDamage(null);

    }

    private void OnTriggerEnter(Collider other)
    {
        OnTakeDamage(other.transform);
    }

    private void OnTakeDamage(Transform collider)
    {
        //TODO 根据other的类型来判断受到的伤害程度

        PropBoom propBoom = GetComponent<PropBoom>();
        if (propBoom != null) propBoom.boomNow();

        if (m_DamagedState != null)
        {
            if (m_DamagedStateTransform == null)
            {
                m_DamagedState = Instantiate(m_DamagedState,
                    transform.position,
                    transform.rotation);
            }
            else
            {
                m_DamagedState = Instantiate(m_DamagedState,
                    m_DamagedStateTransform.position,
                    m_DamagedStateTransform.rotation);
            }
            m_DamagedState.transform.parent = null;
        }
        Destroy(gameObject);
    }
}
